import scenejson from "./scene.json";

let aa = {
  data() {
    return {};
  },
  methods: {
    earthopt() {
      earth.xbsjFromJSON(scenejson);

      // 113.562211 34.808985
      // 113.549628 34.808952
      // 113.549574 34.806160
      // 113.562248 34.806202

      let arr = [
        113.562211, 34.808985, 113.549628, 34.808952, 113.549574, 34.80616,
        113.562248, 34.806202, 113.562211, 34.808985,
      ];
      // let arr = [
      //   113.562211,34.803985,30,
      //   113.549628,34.808952,30,
      //   113.549574,34.806160,30,
      //   113.562248,34.806202,30,

      // ]

      let viewer = earth.czm.viewer;

      // 创建帧缓冲对象
      var framebuffer = new Cesium.Framebuffer({
        context: viewer.scene.context,
        colorTextures: [
          new Cesium.Texture({
            context: viewer.scene.context,
            width: viewer.canvas.clientWidth,
            height: viewer.canvas.clientHeight,
          }),
        ],
        destroyAttachments: false,
      });

      // 渲染场景到帧缓冲对象
      function renderToFramebuffer() {
        framebuffer.bind();
        viewer.render();
        framebuffer.unbind();
      }



      // 在每帧更新时渲染到帧缓冲对象
      // viewer.clock.onTick.addEventListener(renderToFramebuffer);










      // 定义顶点着色器
      var vertexShaderSource = `
attribute vec3 position;
uniform mat4 modelViewProjection;
varying vec3 vPosition;

void main() {
    gl_Position = modelViewProjection * vec4(position, 1.0);
    vPosition = position;
}
`;

      // 定义片段着色器
      var fragmentShaderSource = `
precision highp float;
varying vec3 vPosition;

void main() {
    // 获取水面倒影颜色
    vec4 reflectionColor = texture2D(reflectionTexture, vec2(vPosition.x, vPosition.z));
    
    // 将水面倒影颜色与多边形的颜色相乘
    gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); // 这里假设多边形颜色为蓝色
}
`;

      // 创建自定义材质
      // var material = Cesium.Material.fromType("Custom");
      // material.shaderSource = {
      //   vertexShaderSource: vertexShaderSource,
      //   fragmentShaderSource: fragmentShaderSource,
      //   uniforms: {
      //     // reflectionTexture: framebuffer.getColorTexture(0), // 设置反射纹理
      //   },
      // };

      let mm = new Cesium.Material({
        fabric: {
          type: 'Color',
          uniforms: {
            color: new Cesium.Color(1.0, 0.0, 0.0, 0.5)
          }
        }
      })

      // 创建多边形实体
      var polygonEntity = viewer.entities.add({
        polygon: {
          hierarchy: Cesium.Cartesian3.fromDegreesArray(arr),
          height: 30,
          material: mm,
        },
      });
    },
  },
};

export default aa;
